using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class PhieuHangTraDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SavePhieuHangTra(PhieuHangTra phieuhangtra)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddPhieuHangTra", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddPhieuHangTra";
            SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieuhangtra.Id);
			cmd.Parameters.Add(param);
            param = CreateParameter("@Prefix", DbType.String, ParameterDirection.Input, phieuhangtra.Prefix);
            cmd.Parameters.Add(param);
            param = CreateParameter("@OrderNumber", DbType.String, ParameterDirection.Input, phieuhangtra.OrderNumber);
            cmd.Parameters.Add(param);
            param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, phieuhangtra.MaSo);
            cmd.Parameters.Add(param);
            param = CreateParameter("@History", DbType.String, ParameterDirection.Input, phieuhangtra.History);
            cmd.Parameters.Add(param);
			param = CreateParameter("@NgayThang", DbType.DateTime, ParameterDirection.Input, phieuhangtra.NgayThang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoChungTu", DbType.String, ParameterDirection.Input, phieuhangtra.SoChungTu);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NguoiLapPhieu", DbType.String, ParameterDirection.Input, phieuhangtra.NguoiLapPhieu);
			cmd.Parameters.Add(param);
			param = CreateParameter("@KhachHang", DbType.String, ParameterDirection.Input, phieuhangtra.KhachHang);
			cmd.Parameters.Add(param);
            param = CreateParameter("@ChiNhanhKhachHang", DbType.String, ParameterDirection.Input, phieuhangtra.ChiNhanhKhachHang);
            cmd.Parameters.Add(param);
			param = CreateParameter("@LyDo", DbType.String, ParameterDirection.Input, phieuhangtra.LyDo);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DiaChi", DbType.String, ParameterDirection.Input, phieuhangtra.DiaChi);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdChiNhanhCongTy", DbType.Int32, ParameterDirection.Input, phieuhangtra.IdChiNhanhCongTy);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TongTienHang", DbType.Int64, ParameterDirection.Input, phieuhangtra.TongTienHang);
			cmd.Parameters.Add(param);
            param = CreateParameter("@ChietKhau", DbType.Int64, ParameterDirection.Input, phieuhangtra.ChietKhau);
            cmd.Parameters.Add(param);
            param = CreateParameter("@TienChietKhau", DbType.Int64, ParameterDirection.Input, phieuhangtra.TienChietKhau);
            cmd.Parameters.Add(param);
            param = CreateParameter("@Thue", DbType.Int64, ParameterDirection.Input, phieuhangtra.Thue);
            cmd.Parameters.Add(param);
            param = CreateParameter("@TienThue", DbType.Int64, ParameterDirection.Input, phieuhangtra.TienThue);
            cmd.Parameters.Add(param);
			param = CreateParameter("@TongCong", DbType.Int64, ParameterDirection.Input, phieuhangtra.TongCong);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, phieuhangtra.IsDelete);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all PhieuHangTra in DB with IsDelete = 0
		public ResultObj GetAllPhieuHangTra( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuHangTra> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllPhieuHangTra", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuHangTra>();
			PhieuHangTra item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuHangTra();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.Prefix = Convert.ToString(reader["Prefix"]);
            item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
            item.MaSo = Convert.ToString(reader["MaSo"]);
            item.History = Convert.ToString(reader["History"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.SoChungTu = Convert.ToString(reader["SoChungTu"]);
			item.NguoiLapPhieu = Convert.ToString(reader["NguoiLapPhieu"]);
			item.KhachHang = Convert.ToString(reader["KhachHang"]);
            item.ChiNhanhKhachHang = Convert.ToString(reader["ChiNhanhKhachHang"]);
			item.LyDo = Convert.ToString(reader["LyDo"]);
			item.DiaChi = Convert.ToString(reader["DiaChi"]);
			item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
			item.TongTienHang = Convert.ToInt64(reader["TongTienHang"]);
            item.ChietKhau = Convert.ToInt64(reader["ChietKhau"]);
            item.TienChietKhau = Convert.ToInt64(reader["TienChietKhau"]);
            item.Thue = Convert.ToInt64(reader["Thue"]);
            item.TienThue = Convert.ToInt64(reader["TienThue"]);
			item.TongCong = Convert.ToInt64(reader["TongCong"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all PhieuHangTra in DB with condition
		public ResultObj GetAllPhieuHangTraByID(PhieuHangTra phieuhangtra)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuHangTra> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllPhieuHangTraByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieuhangtra.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuHangTra>();
			PhieuHangTra item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuHangTra();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.Prefix = Convert.ToString(reader["Prefix"]);
            item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
            item.MaSo = Convert.ToString(reader["MaSo"]);
            item.History = Convert.ToString(reader["History"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.SoChungTu = Convert.ToString(reader["SoChungTu"]);
			item.NguoiLapPhieu = Convert.ToString(reader["NguoiLapPhieu"]);
			item.KhachHang = Convert.ToString(reader["KhachHang"]);
            item.ChiNhanhKhachHang = Convert.ToString(reader["ChiNhanhKhachHang"]);
			item.LyDo = Convert.ToString(reader["LyDo"]);
			item.DiaChi = Convert.ToString(reader["DiaChi"]);
			item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
			item.TongTienHang = Convert.ToInt64(reader["TongTienHang"]);
            item.ChietKhau = Convert.ToInt64(reader["ChietKhau"]);
            item.TienChietKhau = Convert.ToInt64(reader["TienChietKhau"]);
            item.Thue = Convert.ToInt64(reader["Thue"]);
            item.TienThue = Convert.ToInt64(reader["TienThue"]);
			item.TongCong = Convert.ToInt64(reader["TongCong"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a PhieuHangTra out of DB with condition
		public ResultObj DeletePhieuHangTra(PhieuHangTra phieuhangtra)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeletePhieuHangTra", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeletePhieuHangTra";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieuhangtra.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of CTPhieuHangTra from DB with condition
		public ResultObj GetCTPhieuHangTra(int IdCTPhieuHangTra)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTPhieuHangTra> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetCTPhieuHangTraByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdCTPhieuHangTra);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTPhieuHangTra>();
			CTPhieuHangTra item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTPhieuHangTra();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdPhieuHangTra = Convert.ToInt32(reader["IdPhieuHangTra"]);
			item.TenHang = Convert.ToString(reader["TenHang"]);
			item.MaHang = Convert.ToString(reader["MaHang"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.DonViTinhQuyDoi = Convert.ToString(reader["DonViTinhQuyDoi"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.SoLuongQuyDoi = Convert.ToInt64(reader["SoLuongQuyDoi"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ThanhTien = Convert.ToInt64(reader["ThanhTien"]);
			item.SoKg = Convert.ToInt64(reader["SoKg"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
					
	}
}
